//package com.cloudpivot.project.rabbitmq.service;
//
//import com.rabbitmq.client.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.context.annotation.Bean;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.util.concurrent.TimeoutException;
//
//@Component
//public class ConsumerService2  {
//    @Value("${rabbitmq-config.host-name}")
//    private String HOST_NAME;
//    @Value("${rabbitmq-config.host-port}")
//    private Integer HOST_PORT;
//    @Value("${rabbitmq-config.username}")
//    private String USER_NAME;
//    @Value("${rabbitmq-config.password}")
//    private String PASSWORD;
////    private static final String QUEUE_NAME = "first_queues";
//    private static final String EXCHANGE_NAME = "amq.direct";
//    private static final String VIRTUAL_HOST = "/test";
//
//
//    @Bean
//    public void firstCumsumerKey1() {
//        try {
////            System.out.println("消费者1开始等待");
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            String queueName = "first_queues_key1";
//            channel.queueDeclare(queueName,true,false,false,null);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("=======================");
//                    System.out.println("获得的消息为(1)=====>>" + message);
//                    System.out.println("该消息对应的参数(1)routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
//                    channel.basicAck(envelope.getDeliveryTag(),false);
//                }
//            };
//            channel.basicConsume(queueName, false, myConsumer);
//        } catch (Exception e){
//            System.out.println("运行异常");
//            throw new RuntimeException(e);
//        }
//
//    }
//
//    @Bean
//    public void firstCumsumerKey2() {
//        try {
////            System.out.println("消费者2开始等待");
//            ConnectionFactory factory=new ConnectionFactory();
//            factory.setHost(HOST_NAME);
//            factory.setPort(HOST_PORT);
//            factory.setUsername(USER_NAME);
//            factory.setPassword(PASSWORD);
//            factory.setVirtualHost(VIRTUAL_HOST);
//            Connection rabbitMQConnection= factory.newConnection();
//            Channel channel = rabbitMQConnection.createChannel();
//            String queueName = "first_queues_key2";
//            channel.queueDeclare(queueName,true,false,false,null);
//            channel.basicQos(1);
//            Consumer myConsumer=new DefaultConsumer(channel){
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    String routingKey = envelope.getRoutingKey();
//                    String contentType = properties.getContentType();
//                    String deliveryTag = String.valueOf(envelope.getDeliveryTag());
//                    String message=new String(body,"UTF-8");
//                    System.out.println("=======================");
//                    System.out.println("获得的消息为(2)=====>>" + message);
//                    System.out.println("该消息对应的参数(2)routingKey:"+routingKey+" deliveryTag:"+deliveryTag+" contentType:"+contentType);
//                    channel.basicAck(envelope.getDeliveryTag(),false);
//                }
//            };
//            channel.basicConsume(queueName, false, myConsumer);
//        } catch (Exception e){
//            System.out.println("运行异常");
//            throw new RuntimeException(e);
//        }
//
//    }
//
//}
